package br.com.dallasmotopecas.comum.fabricante.processamento;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import br.com.dallasmotopecas.comum.fabricante.controller.FabricanteController;
import br.com.dallasmotopecas.seguranca.processamento.controller.ProcessamentoController;
import br.com.dallasmotopecas.seguranca.processamento.vo.ProcessamentoVo;
import br.com.dallasmotopecas.util.exception.CampoObrigatorioException;
import br.com.dallasmotopecas.util.exception.ClasseNaoEncontradaException;
import br.com.dallasmotopecas.util.exception.ConexaoException;
import br.com.dallasmotopecas.util.exception.ConsultarObjetoException;
import br.com.dallasmotopecas.util.exception.InserirObjetoException;
import br.com.dallasmotopecas.util.exception.ListarObjetoException;

public class ProcessamentoFabricante implements ServletContextListener, Serializable
{
	private static final long serialVersionUID = 1L;
	private ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
	private Timer timer;

	@Override
	public void contextDestroyed(ServletContextEvent arg0)
	{
		
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0)
	{
		ProcessamentoController processamentoController = null;
		ProcessamentoVo processamentoVo = null;
		Calendar calendar = null;
		Date time = null;
		try
		{
			processamentoVo = new ProcessamentoVo("processamento fabricante");
			processamentoController = new ProcessamentoController(externalContext);
			processamentoController.consultarProcessamento(processamentoVo);
			calendar = Calendar.getInstance();
			calendar.setTime(new Date(processamentoVo.getHorario().getTime()));
			
			time = calendar.getTime();
			
			timer = new Timer();
			
			timer.schedule(new TimerTask()
			{	
				@Override
				public void run()
				{
					carregarProcessamentoFabricante();
				}
			}, time);
			
		}
		catch(ClasseNaoEncontradaException e)
		{
			e.printStackTrace();
		}
		catch (ConexaoException e)
		{
			e.printStackTrace();
		}
		catch (CampoObrigatorioException e)
		{
			e.printStackTrace();
		}
		catch (ConsultarObjetoException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (processamentoController != null)
			{
				processamentoController.close();
				processamentoController = null;
			}
		}
	}
	
	private void carregarProcessamentoFabricante()
	{
		FabricanteController fabricanteController = null;
		try
		{
			fabricanteController = new FabricanteController(externalContext);
			fabricanteController.processarFabricante();
			System.out.println("Fabricante OK");
		}
		catch(ClasseNaoEncontradaException e)
		{
			e.printStackTrace();
		}
		catch (ConexaoException e)
		{
			e.printStackTrace();
		}
		catch (ListarObjetoException e)
		{
			e.printStackTrace();
		}
		catch (InserirObjetoException e)
		{
			e.printStackTrace();
		}
		finally
		{
			fabricanteController = null;
		}
	}
}
